package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private SetMealDishService setMealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishService dishService;
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setMealDto)
    {
        log.info("套餐信息: {}",setMealDto);
        String key="setMeal_"+setMealDto.getCategoryId();
        redisTemplate.delete(key);
        setMealService.saveWithDish(setMealDto);
        return R.success("套餐添加成功");
    }
    @GetMapping
    public Setmeal getById(Long id)
    {
        LambdaQueryWrapper<Setmeal>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getId,id);
        Setmeal setmeal = setMealService.getOne(lambdaQueryWrapper);
        return setmeal;

    }
    @GetMapping("/list")
    public R<List<Setmeal>>list(Setmeal setmeal)
    {
        List<Setmeal> list=null;
        String key="setMeal_"+setmeal.getCategoryId();
        List<Setmeal>setmealList = (List<Setmeal>) redisTemplate.opsForValue().get(key);
        if(setmealList!=null)
        {
            return R.success(setmealList);
        }
        log.info("setmeal:"+setmeal);
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(null!=setmeal.getCategoryId(),Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(null!=setmeal.getStatus(),Setmeal::getStatus,setmeal.getStatus());
        list = setMealService.list(queryWrapper);
        redisTemplate.opsForValue().set(key,list,60,TimeUnit.MINUTES);
        return R.success(list);
    }

    @GetMapping("/page")
    public R<Page>page(int page,int pageSize,String name)
    {
        Page<Setmeal>pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto>dtoPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setMealService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records=pageInfo.getRecords();
        List<SetmealDto>dtoRecords=records.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!=null)
            {
                String name1 = category.getName();
                setmealDto.setCategoryName(name1);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(dtoRecords);
        return R.success(dtoPage);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long>ids)
    {
        log.info("ids:"+ids);
        for (Long id : ids) {
            Setmeal setmeal = setMealService.getById(id);
            String key="setMeal_"+setmeal.getCategoryId();
            redisTemplate.delete(key);
        }
        setMealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
    @PostMapping("/status/1")
    public R<String>startSales(@RequestParam List<Long>ids)
    {

        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper= null;
        for (Long i:ids) {
            lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(Setmeal::getId,i);
            lambdaUpdateWrapper.set(Setmeal::getStatus,1);
            setMealService.update(lambdaUpdateWrapper);
            Setmeal setmeal = setMealService.getById(i);
            String key="setMeal_"+setmeal.getCategoryId();
            redisTemplate.delete(key);
        }
        return R.success("起售成功");
    }
    @PostMapping("/status/0")
    public R<String>stopSales(@RequestParam List<Long>ids)
    {
        LambdaUpdateWrapper<Setmeal>lambdaUpdateWrapper= null;
        for (Long i:ids) {
            lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(Setmeal::getId,i);
            lambdaUpdateWrapper.set(Setmeal::getStatus,0);
            setMealService.update(lambdaUpdateWrapper);
            Setmeal setmeal = setMealService.getById(i);
            String key="setMeal_"+setmeal.getCategoryId();
            redisTemplate.delete(key);
        }
        return R.success("停售成功");
    }
    @RequestMapping(value = "/dish/{id}",method = RequestMethod.GET)
    public R<List<SetmealDishDto>>getDish(@PathVariable Long id)
    {
        /*Setmeal setmeal = setMealService.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> dishList = setMealDishService.list(lambdaQueryWrapper);
        setmealDto.setSetmealDishes(dishList);*/
        LambdaQueryWrapper<SetmealDish>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setMealDishService.list(lambdaQueryWrapper);
        List<SetmealDishDto>dtoList=list.stream().map((item)->
        {
            SetmealDishDto dishDto=new SetmealDishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long dishId=item.getDishId();
            Dish dish = dishService.getById(dishId);
            dishDto.setImage(dish.getImage());
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dtoList);
    }
}
